package main

import (
	"os"
	"fmt"
	"go-glue.googlecode.com/hg/rlglue"
)

func main() {
	var err os.Error
	defer func() {
		if err != nil {
			panic(err)
		}
	}()
	gt := &GlueTester{"test_1", 0, 0}
	ex := new(rlglue.Experiment)
	if err = ex.AutoConnect(); err != nil {
		return
	}
	if _, err = ex.Init(); err != nil {
		return
	}
	if _, _, err = ex.Start(); err != nil {
		return
	}
	var obs rlglue.Observation
	var r float64
	var t bool
	var reply string
	if obs, _, r, t, err = ex.Step(); err != nil {
		return
	}
	reply, err = ex.EnvironmentMessage("one")
	gt.check("one|1.|one" != reply)
	reply, err = ex.AgentMessage("one")
	gt.check("one|1.|one" != reply)
	gt.check(t != false)
	gt.check(r != 1.0)
	gt.check(len(obs.Ints()) != 1)
	gt.check(len(obs.Doubles()) != 0)
	gt.check(len(obs.Chars()) != 0)
	gt.check(obs.Ints()[0] != 0)
	if obs, _, r, t, err = ex.Step(); err != nil {
		return
	}
	reply, err = ex.EnvironmentMessage("two")
	gt.check("two|2.2.|two" != reply)
	reply, err = ex.AgentMessage("two")
	gt.check("two|2.2.|two" != reply)
	gt.check(t != false)
	gt.check(r != 1.0)
	gt.check(len(obs.Ints()) != 1)
	gt.check(len(obs.Doubles()) != 0)
	gt.check(len(obs.Chars()) != 0)
	gt.check(obs.Ints()[0] != 1)
	if obs, _, r, t, err = ex.Step(); err != nil {
		return
	}
	reply, err = ex.EnvironmentMessage("three")
	gt.check("three||three" != reply)
	reply, err = ex.AgentMessage("three")
	gt.check("three||three" != reply)
	gt.check(t != false)
	gt.check(r != 1.0)
	gt.check(len(obs.Ints()) != 1)
	gt.check(len(obs.Doubles()) != 0)
	gt.check(len(obs.Chars()) != 0)
	gt.check(obs.Ints()[0] != 2)
	if obs, _, r, t, err = ex.Step(); err != nil {
		return
	}
	reply, err = ex.EnvironmentMessage("four")
	gt.check("four|4.|four" != reply)
	reply, err = ex.AgentMessage("four")
	gt.check("four|4.|four" != reply)
	gt.check(t != false)
	gt.check(r != 1.0)
	gt.check(len(obs.Ints()) != 1)
	gt.check(len(obs.Doubles()) != 0)
	gt.check(len(obs.Chars()) != 0)
	gt.check(obs.Ints()[0] != 3)
	if obs, _, r, t, err = ex.Step(); err != nil {
		return
	}
	reply, err = ex.EnvironmentMessage("five")
	gt.check("five|5.5.|five" != reply)
	reply, err = ex.AgentMessage("five")
	gt.check("five|4.|five" != reply)
	gt.check(t == false)
	gt.check(r != 1.0)
	if obs, _, r, t, err = ex.Step(); err != nil {
		return
	}
	gt.check(t != false)
	gt.check(r != -2.0)
	gt.check(len(obs.Ints()) != 5)
	gt.check(len(obs.Doubles()) != 5)
	gt.check(len(obs.Chars()) != 5)
	gt.check(obs.Ints()[0] != 173)
	gt.check(obs.Ints()[1] != -173)
	gt.check(obs.Ints()[2] != 2147483647)
	gt.check(obs.Ints()[3] != 0)
	gt.check(obs.Ints()[4] != -2147483648)
	gt.check(obs.Doubles()[0] != 0.0078125)
	gt.check(obs.Doubles()[1] != -0.0078125)
	gt.check(obs.Doubles()[2] != 0.0)
	gt.check(obs.Doubles()[3] != 0.0078125e150)
	gt.check(obs.Doubles()[4] != -0.0078125e150)
	gt.check(obs.Chars()[0] != 'g')
	gt.check(obs.Chars()[1] != 'F')
	gt.check(obs.Chars()[2] != '?')
	gt.check(obs.Chars()[3] != ' ')
	gt.check(obs.Chars()[4] != '&')
	if err = ex.Cleanup(); err != nil {
		return
	}
	if err = ex.Close(); err != nil {
		return
	}
	fmt.Printf("%v\n", gt)
	os.Exit(gt.failCount)
}
